import bb.cascades 1.0

NavigationPane {
    id: homeNavigation
    backButtonsVisible: false
    peekEnabled: false
    
    Page {
        
        actions: [
            // define the actions for first tab here
            ActionItem {
                title: lang.side_setting
                imageSource: "asset:///images/setting/setting.png"
                onTriggered: {
                    var page = settingPage.createObject() ;
                    homeNavigation.push(page);
                    //                imgTab1.rotationZ = imgTab1.rotationZ + 90;
                }
            },
            ActionItem {
                title: lang.side_help
                imageSource: "asset:///images/help/ic_help.png"
                onTriggered: {
                    var page = helpPage.createObject() ;
                    homeNavigation.push(page);
                    //                    langDialog.open();
                    //                imgTab1.imageSource = "asset:///images/picture1br.png";
                }
            },
            ActionItem {
                title: lang.side_about
                imageSource: "asset:///images/help/ic_info.png"
                onTriggered: {
                    var page = aboutPage.createObject() ;
                    homeNavigation.push(page);
                    //                    langDialog.open();
                    //                imgTab1.imageSource = "asset:///images/picture1br.png";
                }
            }
        ]
        
        Container {
            Container {
                horizontalAlignment: HorizontalAlignment.Center
                topPadding: 0
                
                ImageView {
                    imageSource: "asset:///images/home/logo.png"
                    horizontalAlignment: HorizontalAlignment.Center
                }
            
            
            }
            
            Container {
                //            layout: StackLayout {
                //                orientation: LeftToRight
                //            }
                horizontalAlignment: HorizontalAlignment.Center
                topPadding: 50
                
                Label {
                    text: lang.home_welcome
                    horizontalAlignment: HorizontalAlignment.Center
                }
                
                //                Label {
                //                    text: "fgdfggd dsgdhf fghdfhs sdgds dgdg sdgasgsdgdsg "
                //                    maxWidth: 650
                //                    horizontalAlignment: HorizontalAlignment.Center
                //                }
                
                Label {
                    text: lang.home_description
                    horizontalAlignment: HorizontalAlignment.Center
                    maxWidth: 660
                    multiline: true
                    
                    textStyle{
                        textAlign: TextAlign.Center
                    }
                }
            }
            
            Divider {
            }
            
            Container {
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                horizontalAlignment: HorizontalAlignment.Center
//                topPadding: 50
                maxWidth: 660
                
                Label {
                    text: lang.home_status
                    
//                    textStyle{
//                        textAlign: TextAlign.Center
//                    }
                }
            }
            
            Divider {
            }
            
            Container {
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                horizontalAlignment: HorizontalAlignment.Center
                topPadding: 20
                maxWidth: 660
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 2
                    }
                    text: lang.home_balance
                }
                
                Label {
                    id: label_balance
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    text: lang.home_no_record
                }
            }
            
            Container {
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                horizontalAlignment: HorizontalAlignment.Center
                //            topPadding: 50
                maxWidth: 660
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 2
                    }
                    text: lang.home_expenses_count
                }
                
                Label {
                    id: label_expenses_count
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    text: lang.home_no_record
                }
            }
            
            Container {
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                horizontalAlignment: HorizontalAlignment.Center
                //            topPadding: 50
                maxWidth: 660
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 2
                    }
                    text: lang.home_income_count
                }
                
                Label {
                    id: label_income_count
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    text: lang.home_no_record
                }
            }
            Container {
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                horizontalAlignment: HorizontalAlignment.Center
//                topPadding: 50
                maxWidth: 660
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 2
                    }
                    text: lang.home_expenses_sum
                }
                
                Label {
                    id: label_expenses_sum
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    text: lang.home_no_record
                }
            }
            
            Container {
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                horizontalAlignment: HorizontalAlignment.Center
                //            topPadding: 50
                maxWidth: 660
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 2
                    }
                    text: lang.home_income_sum
                }
                
                Label {
                    id: label_income_sum
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    text: lang.home_no_record
                }
            }
            
            Container {
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                horizontalAlignment: HorizontalAlignment.Center
                //            topPadding: 50
                maxWidth: 660
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 2
                    }
                    text: lang.home_latest_record
                }
                
                Label {
                    id: label_latest_record
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    text: lang.home_no_record
                }
            }
        }
    }
    
    onCreationCompleted: {
        // this slot is called when declarative scene is created
        // write post creation initialization here
        console.log("home - onCreationCompleted()")
        
        var getlang = backend.getLang() ;
        if (getlang == ""){
            var guidepage = firstGuide.createObject();
            homeNavigation.push(guidepage);
        }else{
            lang.lang = getlang ;
        }
        
        updateStatus() ;
//        console.log("user balance : " + JSON.stringify(database.GetUserAsset(0)));
//        console.log("user GetPaymentCount : " + database.GetPaymentCount());
//        console.log("user GetIncomeCount : " + database.GetIncomeCount());
//        console.log("user GetPaymentSum : " + database.GetPaymentSum());
//        console.log("user GetIncomeSum : " + database.GetIncomeSum());
//        console.log("user GetLastDate : " + database.GetLastDate());

        
    }
    
    function updateStatus() {
//        console.log("user balance : " + JSON.stringify(database.GetUserAsset(0)));
        var rt0 = database.GetUserAsset(0) ;
        if (rt0 != 0){
            label_balance.text = rt0 ;
        }
        label_expenses_count.text = database.GetPaymentCount();
        label_income_count.text = database.GetIncomeCount() ;
        label_expenses_sum.text = database.GetPaymentSum() ;
        label_income_sum.text = database.GetIncomeSum() ;
        var time = database.GetLastDate() ;
        console.log("isNaN( time.getTime() ):  " + isNaN( time.getTime() ));
//        if (JSON.stringify(time) != "Invalid Date") {
    
        if (!isNaN( time.getTime() ) ) {
            label_latest_record.text = time.getFullYear() + " - " + (time.getMonth() + 1) + " - " + time.getDate();
        }
        else {
            label_latest_record.text = lang.home_no_record ;
        }
    
    }
    
    onPopTransitionEnded: {
        updateStatus() ;
//        chartDetailPage.destroy();
    }
    
    attachedObjects: [
        ComponentDefinition {                      
            id: firstGuide                       
            source: "../setting/LangSelect.qml"
        },
        ComponentDefinition {                      
            id: aboutPage                       
            source: "../setting/About.qml"
        },
        ComponentDefinition {                      
            id: helpPage                       
            source: "../help/HelpA.qml"
        },
        ComponentDefinition {                      
            id: settingPage                       
            source: "../setting/Setting.qml"
        }
    ]
}